package demo1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: liangyuanzhang
 * Date: 2025-3-10
 * Time: 23:47
 */
/*class MyArray{
    public Object[] array = new Object[10];

    public void setValue(int pos,Object val){
        array[pos] = val;
    }

    public Object getVal(int pos){
        return array[pos];
    }
}*/

        // <E> 占位符:表示当前类是一个泛型类
class MyArray<E>{
    public Object[] array = new Object[10];

    public void setValue(int pos,E val){
        array[pos] = val;
    }

    public E getVal(int pos){
        return (E)array[pos];
    }
}
public class Test2 {
    public static void main(String[] args) {
        MyArray<Integer> myArray = new MyArray<Integer>();
        myArray.setValue(0,10);
        myArray.setValue(1,20);
        //myArray.setValue(1,"hello");// 自动类型检查
        Integer str = myArray.getVal(1);// 自动类型转换
        System.out.println(str);

        System.out.println("===========");
        MyArray<String> myArray2 = new MyArray<String>();
        myArray2.setValue(0,"字符串");
        myArray2.setValue(1,"abc");
        String str2 = myArray2.getVal(1);
        System.out.println(str2);
    }
    public static void main1(String[] args) {
        MyArray myArray = new MyArray();
        myArray.setValue(0,10);
        myArray.setValue(1,"hello");


        String str = (String) myArray.getVal(1);
        System.out.println(str);
    }
}
